package com.taxinfo.controller;

import cn.hutool.core.lang.Snowflake;
import com.taxinfo.AbstractApp;
import com.taxinfo.component.alert.MyAlert;
import com.taxinfo.pojo.dto.LoginDto;
import com.taxinfo.service.LoginService;
import com.taxinfo.service.MainReportService;
import com.taxinfo.utils.SaltUtil;
import com.taxinfo.utils.SpringUtil;
import com.taxinfo.view.WelcomeView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class ForgetPasswdController implements Initializable {
    @FXML
    public VBox vbRoot;
    @FXML
    public TextField tf_username;
    @FXML
    public PasswordField pf_password;
    @FXML
    public PasswordField pf_passwordOk;
    @FXML
    public TextField tf_VerifyCode;
    @FXML
    public Button btn_getCode;
    @FXML
    public TextField tf_email;
    @FXML
    public Button btn_return;
    @FXML
    public Button btn_update;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        tf_username.clear();
        pf_password.clear();
        pf_passwordOk.clear();
        tf_VerifyCode.clear();
        tf_email.clear();
    }

    @FXML
    private void btn_update() {
        ForgetPassWordAction forgetPassWordAction = new ForgetPassWordAction();
        if(StringUtils.isEmpty(tf_username.getText().trim())){
            new MyAlert(Alert.AlertType.WARNING, "用户名不能为空").show();
            return;
        }
        if(StringUtils.isEmpty(tf_VerifyCode.getText().trim())){
            new MyAlert(Alert.AlertType.WARNING, "验证码不能为空").show();
            return;
        }
        boolean blnFlg = forgetPassWordAction.verfyCode(tf_VerifyCode.getText().trim(),tf_username.getText().trim());
        if(!blnFlg){
            new MyAlert(Alert.AlertType.WARNING, "验证码错误").show();
            return;
        }else{
            if(StringUtils.isEmpty(pf_password.getText().trim())){
                new MyAlert(Alert.AlertType.WARNING, "新密码不能为空").show();
                return;
            }
            if(StringUtils.isEmpty(pf_passwordOk.getText().trim())){
                new MyAlert(Alert.AlertType.WARNING, "确认新密码不能为空").show();
                return;
            }
            if(pf_password.getText().trim().equals(pf_passwordOk.getText().trim())){

                String password = new SaltUtil(tf_username.getText().trim(), "MD5").encode(pf_passwordOk.getText().trim());
                forgetPassWordAction.updatePassword(password);
                new MyAlert(Alert.AlertType.INFORMATION, "密码修改成功").show();
                AbstractApp.showView(WelcomeView.class);

            }

        }

    }

    @FXML
    private void btn_return() {
        AbstractApp.showView(WelcomeView.class);
    }

    @FXML
    private void btn_getCode() {
        ForgetPassWordAction forgetPassWordAction = new ForgetPassWordAction();
        forgetPassWordAction.sendMessage();
   }

    /**
     * 忘记密码处理
     */
    private  class ForgetPassWordAction{

        LoginService service = (LoginService) SpringUtil.getBean("LoginServiceImpl");

        public void sendMessage(){
            if(StringUtils.isEmpty(tf_username.getText())){
                new MyAlert(Alert.AlertType.WARNING, "用户名不能为空").show();
                return;
            }
            if(StringUtils.isEmpty(tf_email.getText())){
                new MyAlert(Alert.AlertType.WARNING, "邮箱不能为空").show();
                return;
            }
            LoginDto loginDto = service.getUserInfo(tf_username.getText().trim());
            if (loginDto!=null){
                if (loginDto.getEmail().equals(tf_email.getText())){
                    Snowflake snowflake = new Snowflake(1, 2);
                    String tmpCode = String.valueOf(snowflake.nextId());
                    String veriCode =tmpCode.substring(tmpCode.length()-6,tmpCode.length());
                    System.out.println(veriCode);
                    boolean msgflg = service.sendMailMsg(loginDto.getEmail(),"获取验证码通知","您正在使用[税智报]系统服务，您的验证码是[ "+veriCode+" ]。请不要将验证码透露给他人，以保障你的账户安全。" );
                    if(msgflg){
                        int num  = service.updateUser(veriCode,tf_username.getText().trim());
                        new MyAlert(Alert.AlertType.INFORMATION, "验证码已发送，请前往邮箱[ "+loginDto.getEmail()+" ]获取!").show();
                    }else{
                        new MyAlert(Alert.AlertType.WARNING, "邮箱发送异常,请确认邮件地址！").show();
                    }

                }else{
                    new MyAlert(Alert.AlertType.WARNING, "邮箱不匹配").show();
                }

            }else{
                new MyAlert(Alert.AlertType.WARNING, "该邮箱未注册").show();
            }

        }


        public boolean verfyCode(String code, String userName){
            return service.verifyCode(code,userName);
        }

        public void updatePassword(String password){
            service.updatePassword(password,tf_username.getText().trim());
        }

    }
}
